
package com.welsy.authoring.command
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.welsy.authoring.business.*;
	import com.welsy.authoring.model.CourseModelLocator;
	import com.welsy.util.Comparator;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.events.FaultEvent;
	import mx.controls.Alert;
	import mx.collections.ICollectionView;
	import mx.collections.Sort;
	import mx.collections.SortField;
	import mx.utils.ArrayUtil;
	import com.welsy.authoring.vo.CourseComponentsVO;
	import com.welsy.authoring.event.GetFinalResultEvent;
	import mx.rpc.IResponder;
	import com.welsy.authoring.event.SaveQuestionResponseEvent;
	import com.welsy.util.NamespaceLocator;
	import com.adobe.cairngorm.commands.SequenceCommand;
	import com.welsy.util.ChainEvent;
	
	/**
	 * @version	$Revision: 1.1 $
	 */
	public class SaveQuestionResponseCommand extends SequenceCommand implements ICommand, IResponder
	{
	
		override public function execute( event : CairngormEvent ): void
		{
			//nextEvent = ChainEvent(event).nextChainedEvent;
			var sqr_eve : SaveQuestionResponseEvent = SaveQuestionResponseEvent( event );
			nextEvent = sqr_eve.nextChainedEvent;
			var delegate : SaveQuestionResponseDelegate = new SaveQuestionResponseDelegate( this );
			delegate.saveQuestionResponse(sqr_eve.userId, sqr_eve.testId, sqr_eve.questionId, sqr_eve.answerId, sqr_eve.score, sqr_eve.percentage, int(sqr_eve.consultationTime));
		}
	
		public function result( event : Object ) : void
		{
			var xres : XMLList = event.result as XMLList;
			trace(xres.toXMLString());
			
			var NS  : Namespace = new Namespace(NamespaceLocator.USERTESTRESULTS_DATATYPES);
			var TAG_SAVEQUESTIONRESPONSERESULT : QName = new QName(NS, "SaveQuestionResponseResult"); 
			var TAG_RESULTVALUE : QName = new QName(NS, "ResultValue"); 
			
			var sqrr_t : XMLList = xres.descendants(TAG_SAVEQUESTIONRESPONSERESULT);
			var resultCode : String = sqrr_t.child(TAG_RESULTVALUE);//should be ok
			
			if(resultCode.toLowerCase().substr(0, 2) =="ko") {
				Alert.show("Errore dal server in SaveQuestionResponse");
			} else {
				if(resultCode.toLowerCase() == "true") {
					executeNextCommand();
				}
			}
			
		}
	
		public function fault( event : Object ) : void
		{
			Alert.show( "SaveQuestionResponse.fault!" );
		}
	}

}